﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.HVAC;
using YArchitech.LIB;

namespace YArchitech.Plumbing
{
	public partial class FormMultiLinePipeSetting : System.Windows.Forms.Form
	{
		public FormMultiLinePipeSetting(Autodesk.Revit.DB.Document doc)
		{
			this.InitializeComponent();
			this.doc = doc;
			base.Icon = Resource.YArchitechIcon;
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (!this.tranStart)
			{
				this.SetMultiLinePipeInfo();
				RefrigerantPipeCreate refrigerantPipeCreate = new RefrigerantPipeCreate();
				if (FormMultiLinePipeSetting.IsOpenTran)
				{
					for (int i = 0; i < 3; i++)
					{
						refrigerantPipeCreate.PipingSystemTypeCreate(i, this.doc);
					}
				}
				else
				{
					Transaction transaction = new Transaction(this.doc, "创建系统类型");
					transaction.Start();
					for (int j = 0; j < 3; j++)
					{
						refrigerantPipeCreate.PipingSystemTypeCreate(j, this.doc);
					}
					transaction.Commit();
				}
			}
			else
			{
				this.SetMultiLinePipeInfo();
				RefrigerantPipeCreate refrigerantPipeCreate2 = new RefrigerantPipeCreate();
				for (int k = 0; k < 3; k++)
				{
					refrigerantPipeCreate2.PipingSystemTypeCreate(k, this.doc);
				}
			}
			this.flag = true;
			base.Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.flag = false;
			base.Close();
		}

		private void FormMultiLinePipeSetting_Load(object sender, EventArgs e)
		{
			this.SetComboboxMultiLinePipeSettingInfo();
			this.SetdgvMultiLinePipeSettingInfo();
		}

		private void SetComboboxMultiLinePipeSettingInfo()
		{
			foreach (LinePatternElement linePatternElement in SystemManageHelper.Get_LinePatternElement(this.doc))
			{
				this.Linear.Items.AddRange(new object[]
				{
					linePatternElement.Name
				});
			}
			this.Linear.Items.Add("默认");
			for (int i = 1; i < 17; i++)
			{
				this.LineWidth.Items.Add(i.ToString());
			}
			this.LineWidth.Items.Add("默认");
		}

		private void SetdgvMultiLinePipeSettingInfo()
		{
			List<List<string>> multiLinePipeSetting = new ClassRefrigerantPipeCreateInfo().GetMultiLinePipeSetting();
			int num = -1;
			foreach (List<string> list in multiLinePipeSetting)
			{
				num++;
				this.dgvMultiLinePipeSetting.Rows.Add();
				for (int i = 0; i < list.Count; i++)
				{
					this.dgvMultiLinePipeSetting.Rows[num].Cells[i].Value = list[i];
				}
			}
			this.SetColorShow();
		}

		private void SetColorShow()
		{
			for (int i = 0; i < 3; i++)
			{
				string text = this.dgvMultiLinePipeSetting[2, i].Value.ToString();
				System.Drawing.Color foreColor = System.Drawing.Color.FromArgb(int.Parse(text.Split(new char[]
				{
					' '
				}).ElementAt(1)), int.Parse(text.Split(new char[]
				{
					' '
				}).ElementAt(2)), int.Parse(text.Split(new char[]
				{
					' '
				}).ElementAt(3)));
				this.dgvMultiLinePipeSetting[2, i].Style.ForeColor = foreColor;
			}
		}

		private void FormMultiLinePipeSetting_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.flag)
			{
				this.SetMultiLinePipeInfo();
			}
		}

		public void SetMultiLinePipeInfo()
		{
			List<List<string>> list = new List<List<string>>();
			for (int i = 0; i < 3; i++)
			{
				List<string> list2 = new List<string>();
				for (int j = 0; j < 7; j++)
				{
					if (this.dgvMultiLinePipeSetting.Rows[i].Cells[j].Value == null)
					{
						list2.Add("");
					}
					else
					{
						list2.Add(this.dgvMultiLinePipeSetting.Rows[i].Cells[j].Value.ToString());
					}
				}
				list.Add(list2);
			}
			new ClassRefrigerantPipeCreateInfo().SetMultiLinePipeSettingInfo(list);
		}

		private void dgvMultiLinePipeSetting_CurrentCellDirtyStateChanged(object sender, EventArgs e)
		{
			this.dgvMultiLinePipeSetting.CommitEdit(DataGridViewDataErrorContexts.Commit);
		}

		private void dgvMultiLinePipeSetting_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.ColumnIndex == 2 && e.RowIndex >= 0)
			{
				ColorDialog colorDialog = new ColorDialog();
				colorDialog.AllowFullOpen = true;
				colorDialog.FullOpen = true;
				System.Drawing.Color xmldataColor = SystemManageHelper.getXMLDataColor(this.dgvMultiLinePipeSetting[2, e.RowIndex].Value.ToString());
				if (!xmldataColor.IsKnownColor)
				{
					int[] array = new int[16];
					array[0] = SystemManageHelper.GetIntAboutColor(xmldataColor);
					colorDialog.CustomColors = array;
				}
				colorDialog.Color = xmldataColor;
				DialogResult dialogResult = colorDialog.ShowDialog();
				if (DialogResult.OK == dialogResult)
				{
					string newRGB = SystemManageHelper.getNewRGB(colorDialog.Color.R);
					string newRGB2 = SystemManageHelper.getNewRGB(colorDialog.Color.G);
					string newRGB3 = SystemManageHelper.getNewRGB(colorDialog.Color.B);
					string str = string.Concat(new string[]
					{
						"RGB ",
						newRGB.ToString(),
						" ",
						newRGB2.ToString(),
						" ",
						newRGB3.ToString()
					});
					this.dgvMultiLinePipeSetting[2, e.RowIndex].Value = "█" + str;
					this.dgvMultiLinePipeSetting[2, e.RowIndex].Style.ForeColor = colorDialog.Color;
					this.dgvMultiLinePipeSetting[2, e.RowIndex].ReadOnly = true;
				}
			}
		}

		private Document doc;

		public bool tranStart;

		private bool flag = true;

		public static bool IsOpenTran = true;
	}
}
